﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using SkinnedModel;
using Microsoft.Xna.Framework.Media;
using System;

namespace whereIsTheDonutGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public abstract class SuperPlayer : DrawableGameComponent, whereIsTheDonutGame.IPlayer
    {
        SoundEffect steps;
        SoundEffectInstance stepsInstance;

        public Vector3 Position { get { return position; } }
        public float Rotation { get { return rotation; } }
        public BoundingBox bbox { get; set; }
        //Model currentModel;
        //AnimationPlayer animationPlayer;
        //Matrix[] _originalBonesMatrix;
        ////float bendX = 0;
        //float bendY = 0;
        //float bendZ = 0;

        protected donutGame game;
        protected float yaw, pitch, PlayerVelocity;
        protected int oldX, oldY;

        protected ICamera camera;
        protected Model player;
        protected Vector3 velocity;
        protected Vector3 position;
        protected Vector3 newPosition;
        protected Vector3 movement;
        protected float rotation, rotationTmp, rotationSpeed;

        public SuperPlayer(donutGame game)
            : base(game)
        {
            this.game = game;
            this.position = new Vector3(128, game.terrain.Height[3, 3] + 5, -128);
            this.rotation = 0f;
            steps = game.Content.Load<SoundEffect>("Sounds/steps");
            stepsInstance = steps.CreateInstance();
            rotationSpeed = 0.1f;
            PlayerVelocity = 1;
        }
       
        public override void Initialize()
        {
            camera = (ICamera)Game.Services.GetService(typeof(ICamera));
            bbox = new BoundingBox(new Vector3(Position.X - 5, Position.Y - 5, Position.Z - 5), new Vector3(Position.X + 5, Position.Y + 5, Position.Z + 5));
            base.Initialize();
        }

        public abstract void UpdateBoundingBox();


        public override void Update(GameTime gameTime)
        {
            //int boneId = currentModel.Bones["TopBone"].Index - 1;
            //// find the matrix index Bone by multiplying by it's original 'BindPose' position
            //animationPlayer.boneTransforms[boneId] = Matrix.CreateFromYawPitchRoll(0, bendY, bendZ) * _originalBonesMatrix[boneId];
            //// update the world position 
            //animationPlayer.UpdateWorldTransforms(Matrix.Identity);
            //// update the skin transforms based on new bone movement and world matrix 
            //animationPlayer.UpdateSkinTransforms();

            base.Update(gameTime);
        }

   

        protected void rotate(float rotationSpeed)
        {
            rotation += rotationSpeed;
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            //Matrix[] bones = animationPlayer.GetSkinTransforms();

            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            foreach (ModelMesh mesh in player.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                     effect.EnableDefaultLighting();
                    effect.View = camera.ViewMatrix;
                  
                    effect.World = Matrix.CreateRotationY(rotation) * Matrix.CreateScale(0.5f) * Matrix.CreateTranslation(Position) * Matrix.CreateTranslation(new Vector3(0, 2, 0));
                    
                    effect.Projection = camera.ProjectionMatrix;
                    
                }
                mesh.Draw();
            }
            base.Draw(gameTime);

        }
        protected override void LoadContent()
        {
            //currentModel = Game.Content.Load<Model>("AnimatedTube");
            player = this.Game.Content.Load<Model>("bike9");
            //SkinningData skinningData = player.Tag as SkinningData;

            //// copy the initial bone transforms to keep for later, all new transforms will be performed
            //// relative to the initial position
            //_originalBonesMatrix = new Matrix[skinningData.BindPose.Count];

            //// make copy of the initial BindPose data by iterating through Bones in the BindPose 
            //// and copying their skinning data into our store of initial info.
            //int currentBone = 0;
            //while (currentBone < skinningData.BindPose.Count)
            //{
            //    _originalBonesMatrix[currentBone] = skinningData.BindPose[currentBone];
            //    currentBone++;
            //}
            //if (skinningData == null)
            //    throw new InvalidOperationException
            //        ("This model does not contain a SkinningData tag.");

            //// Create an animation player
            //animationPlayer = new AnimationPlayer(skinningData);

            //// Copy our bonetransforms into our model, displaying our movement
            //skinningData.BindPose.CopyTo(animationPlayer.boneTransforms, 0);

        }
    }
}